package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.ErrorLogs;
import com.example.mall.dto.ErrorLogsDto;
import com.example.mall.mapper.ErrorLogsMapper;
import com.example.mall.query.ErrorLogsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IErrorLogsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 错误日志表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class ErrorLogsServiceImpl extends ServiceImpl<ErrorLogsMapper, ErrorLogs> implements IErrorLogsService {

    @Autowired
    private ErrorLogsMapper errorLogsMapper;

    @Override
    public PageDTO<ErrorLogsDto> queryPage(ErrorLogsQuery errorLogsQuery) {
        if (errorLogsQuery == null) {
            errorLogsQuery = new ErrorLogsQuery();
        }
        Page<ErrorLogs> page = errorLogsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<ErrorLogs> cartPage = lambdaQuery()
                .like(errorLogsQuery.getErrorId()!= null, ErrorLogs::getErrorId, errorLogsQuery.getErrorId())
                .like(errorLogsQuery.getErrorCode()!= null, ErrorLogs::getErrorCode, errorLogsQuery.getErrorCode())
                .like(errorLogsQuery.getErrorMessage()!= null, ErrorLogs::getErrorMessage, errorLogsQuery.getErrorMessage())
                .like(errorLogsQuery.getStackTrace()!= null, ErrorLogs::getErrorId, errorLogsQuery.getErrorId())
                .like(errorLogsQuery.getErrorId()!= null, ErrorLogs::getErrorId, errorLogsQuery.getErrorId())
                .orderByDesc(ErrorLogs::getCreateDate)
                .page(page);
        return PageDTO.of(cartPage, ErrorLogsDto.class);

    }

    @Override
    public void insert(ErrorLogsDto errorLogsDto) {
        ErrorLogs errorLogs = new ErrorLogs();
        CopyUtils.copyProperties(errorLogsDto, errorLogs);
        errorLogs.setErrorId(CodeGenerateUtils.generateUniqueId());
        errorLogs.setCreateDate(new Date());
        errorLogsMapper.insert(errorLogs);
    }

    @Override
    public void update(ErrorLogsDto errorLogsDto) {
        AssertUtil.isNotNull(errorLogsDto.getErrorId(), "errorId不能为空");
        ErrorLogs errorLogs = errorLogsMapper.selectById(errorLogsDto.getErrorId());
        AssertUtil.isNotNull(errorLogs, "errorId不存在");
        CopyUtils.copyProperties(errorLogsDto, errorLogs);
        errorLogsMapper.updateById(errorLogs);
    }

    @Override
    public void delete(Long errorId) {
        AssertUtil.isNotNull(errorId, "errorId不能为空");
        ErrorLogs errorLogs = errorLogsMapper.selectById(errorId);
        AssertUtil.isNotNull(errorLogs, "errorId不存在");
        errorLogsMapper.deleteById(errorId);
    }

    @Override
    public void deleteBatch(List<Long> errorIds) {
        AssertUtil.isNotNull(errorIds, "errorIds不能为空");
        List<ErrorLogs> errorLogs = errorLogsMapper.selectBatchIds(errorIds);
        AssertUtil.isNotNull(errorLogs, "errorIds不存在");
        errorLogsMapper.deleteBatchIds(errorIds);
    }

    @Override
    public ErrorLogs queryErrorLogsById(Long errorId) {
        AssertUtil.isNotNull(errorId, "errorId不能为空");
        return errorLogsMapper.selectById(errorId);
    }

    @Override
    public List<ErrorLogs> queryErrorLogsList() {
        return this.list();
    }
}
